#!/bin/bash
# Somewhat based on makefusiondebs by Trevino(3v1n0)

OPTIONS="bfd-options"

# == standard options
PACKAGER="Trevi - 3v1n0"
PACKAGER_EMAIL="trevi55@gmail.com"
PACKAGE_SUFFIX="3v1ubuntu"

SOURCES="src"
DEBS="debs"
DEBS_OLD="debs-old"
CHANGEDIR="changelogs"
DISTRODIR="compiz-fusion-debian"
PATCH_XCB="false"
INSTALL_DEPS="true"
GIT="git://anongit.compiz-fusion.org"

# == default list of git items
ITEMS="fusion/libraries/bcop"
ITEMS="$ITEMS fusion/plugins-main fusion/plugins-extra fusion/plugins-unsupported"
ITEMS="$ITEMS fusion/compizconfig/libcompizconfig"
ITEMS="$ITEMS fusion/compizconfig/compizconfig-python"
ITEMS="$ITEMS fusion/compizconfig/ccsm"
ITEMS="$ITEMS fusion/decorators/emerald"
ITEMS="$ITEMS fusion/decorators/emerald-themes"

# == server and location of compiz
COMPIZ_SERVER="git://git.freedesktop.org/"
COMPIZ="xorg/app/compiz"

[ ! -f "bfd-options" ] || . bfd-options

case $PATCH_XCB in
	true|yes|1|on)
		MY_DEPS="build-essential libxcomposite-dev libpng12-dev libsm-dev libxrandr-dev libxdamage-dev libxinerama-dev libstartup-notification0-dev libgconf2-dev libgl1-mesa-dev libglu1-mesa-dev libmetacity-dev librsvg2-dev libdbus-1-dev libdbus-glib-1-dev libgnome-desktop-dev libgnome-window-settings-dev curl autoconf automake automake1.9 libtool intltool libxslt1-dev xsltproc libwnck-dev python-dev pyrex-mode python-pyrex x11proto-scrnsaver-dev libxss-dev xorg-dev kde-devel kde4-devel"
		;;
	*)
		MY_DEPS="build-essential libxcomposite-dev libpng12-dev libsm-dev libxrandr-dev libxdamage-dev libxinerama-dev libstartup-notification0-dev libgconf2-dev libgl1-mesa-dev libglu1-mesa-dev libmetacity-dev librsvg2-dev libdbus-1-dev libdbus-glib-1-dev libgnome-desktop-dev libgnome-window-settings-dev curl autoconf automake automake1.9 libtool intltool libxslt1-dev xsltproc libwnck-dev python-dev libX11-xcb-dev pyrex-mode python-pyrex x11proto-scrnsaver-dev libxss-dev xorg-dev kde-devel kde4-devel"
		;;
esac

ARGS_SCRIPT=$*
MYPREFIX="/usr"

# == command to build compiz
autogenpkg_cmd="./autogen.sh --prefix=$MYPREFIX"
buildpkg_cmd="fakeroot dpkg-buildpackage -b -us -uc"
repkg_cmd="fakeroot ./debian/rules binary"
patch_cmd="fakeroot ./debian/rules apply-patches"
unpatch_cmd="fakeroot ./debian/rules reverse-patches"

# == ask the user what to do
function bail_ask {
echo -e "$1"
read -p " [Y/n]: " -n1 continue
if [ "$(echo $continue | tr [:upper:] [:lower:])" = "y" -o "$continue" = "" ]; then
	echo
	[ x$2 == x"quit" ] && exit 2
else
	return 2
fi
echo
}

# == check for an app
function check_sw {
	BIN="$1"
	PACKAGE="$2"
	# == if package name isn't provided, assume that package name is binary name
	[ -n $PACKAGE ] || PACKAGE=$BIN
	# == check for existance
	if [ -z "$(which $BIN)" ]; then
		echo -e "\nYou must install the '$PACKAGE' package in order to use needed git tools\n"
		exit 2
	fi
}

# == download an item from git
function get_repo {
b=`echo $1 | sed 's/.*\///g'`
here=$PWD
if [ -d $b ]; then
	cd $b
	echo "Updating $b"
	git-checkout
	git-pull &> /dev/null || bail_ask "Failed to update $1 !\n Abort?" quit
	cd $here
else
	echo "Cloning $b";
	git-clone $2$1 &> /dev/null || bail_ask "Failed to clone $1 !" quit
fi
}

check_sw "patch"
check_sw "fakeroot"
check_sw "dpkg-buildpackage" "dpkg-dev"
check_sw "dh_builddeb" "debhelper"
check_sw "git-clone" "git-core"
check_sw "git-pull" "git-core"

# == if the root powers are unavailable, ask to store the sudo password
if [ "$(whoami)" != "root" ]; then
	echo -e "\nThis tool could need the 'sudo' password to install some packages it builds, to avoid the script to stop itself to ask the password, you could define it here. It will be saved and used ONLY to install packages (as you can see from the code)."
	if (bail_ask "Would you like to enter it now?"); then
		COUNT=0
		SUDO_PWD_SET="false"
		while [ "$SUDO_PWD_SET" != "true" ] && [ $COUNT -lt 3 ]; do
			read -s -p "Password:" SUDOPWD
			echo ''
			sudo -k
			COUNT=$(($COUNT+1))
			if (echo $SUDOPWD | sudo -S echo -n "" 2> /dev/null); then
				SUDO_PWD_SET="true"
			fi
		done
		unset COUNT
		if [ "$SUDO_PWD_SET" = "false" ]; then
			echo -e "\nThe password you provided seems to be wrong, so it will be re-asked when needed"
			sudo -k
			sleep 2
		fi
	fi
fi

case $INSTALL_DEPS in
	true|on|1|yes)
		if [ x`whoami` == x"root" ]; then
			aptitude install $MY_DEPS
		elif [ x$SUDO_PWD_SET != x"false" ]
			echo $SUDOPWD | sudo -S aptitude install $MY_DEPS
		else
			sudo aptitude install $MY_DEPS
		fi
		;;
	*)
		true
		;;
esac

# == get all items
OLDDIR=$PWD
cd $SOURCES
get_repo $COMPIZ $COMPIZ_SERVER
for singleitem in $ITEMS; do
	get_repo $singleitem $GIT
done
cd $OLDDIR

